#define NOMINMAX
#include <string>
#include <iostream>
#include <filesystem>
#include <windows.h>
#include "NanoCefApp.h"
#include "NanoBrowserWindow.h"

#pragma comment(lib, "./libcef.lib")
#pragma comment(lib, "./libcef_dll_wrapper.lib")

using namespace std::literals;

/*

class NanoCefApp : public CefApp
{
    IMPLEMENT_REFCOUNTING(NanoCefApp);
};

class NanoCefClient : public CefClient, public CefLifeSpanHandler
{
public:
    CefRefPtr<CefBrowser> GetBrowser()
    {
        return pBrowser_;
    }

    CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override
    {
        return this;
    }

    void OnAfterCreated(CefRefPtr<CefBrowser> pBrowser) override
    {
        pBrowser_ = pBrowser;
    }

private:
    CefRefPtr<CefBrowser> pBrowser_;
    IMPLEMENT_REFCOUNTING(CefClient);
};

CefRefPtr<NanoCefClient> pClient;
*/


int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) {
    const CefMainArgs mainAtrgs{ hInst };
    CefRefPtr<NanoCefApp> pApp = new NanoCefApp();
    if (const auto code = CefExecuteProcess(mainAtrgs, pApp, nullptr); code >= 0) {
        return (int)code;
    }

    CefSettings settings;
    settings.no_sandbox = true;
    settings.multi_threaded_message_loop = true;
    CefString(&settings.cache_path).FromString((std::filesystem::current_path() / "cef_cache"s).string());
    CefInitialize(mainAtrgs, settings, pApp, nullptr);

    CreateBrowserWindow(hInst);

    MSG msg;
    while (GetMessageA(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessageA(&msg);
    }

    CleanupBrowserWindow(hInst);
    CefShutdown();

    return (int)msg.wParam;
}
